﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
namespace SZSLSystem.DAL
{
    public class GetPager
    {
        /// <summary>
        /// 根据strWhere条件获取表的记录总数
        /// </summary>
        /// <param name="FieldID">表的唯一字段</param>
        /// <param name="DataTableName">表名</param>
        /// <param name="strWhere">条件</param>
        /// <returns>int</returns>
        public int getCount(string FieldID, string DataTableName, string strWhere)
        {
            int getSum = 0;
            if (string.IsNullOrEmpty(strWhere.Trim()))
            {
                getSum = (int)DbHelperOleDb.GetSingle("Select count(" + FieldID + ") From " + DataTableName);
            }
            else
            {
                string a = "Select count(" + FieldID + ") From " + DataTableName + " Where " + strWhere;
                getSum = (int)DbHelperOleDb.GetSingle(a);
            }
            return getSum;
        }

       

        public DataSet GetWherePagelist(string FieldID, string DataTableName, int pageSize, int currentPageIndex, string strWhere, string OrderFieldID, int OrderFlag)
        {
            string strOrder = " desc ";
            StringBuilder strSql = new StringBuilder();
            if (OrderFlag == 0)
            {
                strOrder = " asc ";
            }
            if (currentPageIndex <= 1)
            {
                strSql.Append("select top " + pageSize + " * FROM " + DataTableName + " where " + strWhere + " order by " + OrderFieldID + strOrder);
            }
            else
            {
                strSql.Append("select top " + pageSize + " *");
                strSql.Append(" FROM " + DataTableName + " where " + FieldID + " not in(select top " + pageSize * (currentPageIndex - 1) + " " + FieldID + " from " + DataTableName + " where " + strWhere + " order by " + OrderFieldID + strOrder + " ) and " + strWhere + " order by " + OrderFieldID + strOrder);

            }
            return DbHelperOleDb.Query(strSql.ToString());

        }


        /// <summary>
        /// 分页GetWherePagelist DataSet
        /// </summary>
        /// <param name="FieldID">表的唯一字段</param>
        /// <param name="DataTableName">表名</param>
        /// <param name="pageSize">页的大小</param>
        /// <param name="currentPageIndex">当前页码</param>
        /// <param name="strWhere">条件</param>
        /// <param name="OrderFieldID">排序字段</param>
        /// <param name="OrderFlag">排序顺序：0 -> asc; 1 -> desc</param>
        /// <returns>DataSet</returns>
        public DataSet GetWherePagelist(string FieldID, string showFields, string DataTableName, int pageSize, int currentPageIndex, string strWhere, string OrderFieldID, int OrderFlag)
        {
            string strOrder = " desc ";
            StringBuilder strSql = new StringBuilder();
            if (OrderFlag == 0)
            {
                strOrder = " asc ";
            }
            if (currentPageIndex <= 1)
            {
                strSql.Append("select top " + pageSize + " " + showFields + " FROM " + DataTableName + " where " + strWhere + " order by " + OrderFieldID+strOrder);
            }
            else
            {
                strSql.Append("select top " + pageSize + " " + showFields);
                strSql.Append(" FROM " + DataTableName + " where " + FieldID + " not in(select top " + pageSize * (currentPageIndex - 1) + " " + FieldID + " from " + DataTableName + " where " + strWhere + " order by " + OrderFieldID + strOrder+ " ) and " + strWhere + " order by " + OrderFieldID+strOrder);
               // strSql.Append(" FROM " + DataTableName + " where " + FieldID + " not in(select top " + pageSize * (currentPageIndex - 1) + " " + FieldID + " from " + DataTableName + " where " + strWhere + " ) and " + strWhere + " order by " + OrderFieldID + strOrder);

            }
            return DbHelperOleDb.Query(strSql.ToString());

        }

        public DataSet GetWherePagelist_zp(int pageSize, int currentPageIndex, string strWhere)
        {
            string strOrder = " desc ";
            StringBuilder strSql = new StringBuilder();
            
            if (currentPageIndex <= 1)
            {
                strSql.Append("SELECT top " + pageSize + "  dbo.t_Comp_Yeji.TGuid, dbo.t_Comp_Yeji.PGuid, dbo.t_Comp_Yeji.Xiangmumingcheng, dbo.t_Comp_Yeji.Xiangmuleixing, dbo.t_Comp_Yeji.Hetongjia,dbo.t_Comp_Yeji.Jiesuanjia, dbo.t_Comp_Yeji.Jishudengji, dbo.t_Comp_Yeji.Hetongduanmingcheng, dbo.t_Comp_Yeji.Kaigongriqi, dbo.t_Comp_Yeji.Jungongriqi, dbo.t_Comp_Yeji.Hetongduankaishizhuanghao, dbo.t_Comp_Yeji.Hetongduanjiesuzhuanghao, dbo.t_Comp_Yeji.zhiliangpingdingqingkuang, dbo.t_Comp_Yeji.suozaishengfen, dbo.t_Comp_Yeji.zhuyaogongchengliang, dbo.t_Comp_Yeji.jianshezhuangtai, dbo.t_Comp_Yeji.xinxitianjiashijian, dbo.t_Comp_Yeji.Gongchengleibie, dbo.t_Comp_Yeji.Chengbaoleixing, dbo.t_Comp_BasicInfo.zhucedi, dbo.t_Comp_BasicInfo.qiyemingcheng FROM dbo.t_Comp_Yeji INNER JOIN dbo.t_Comp_BasicInfo ON dbo.t_Comp_Yeji.PGuid = dbo.t_Comp_BasicInfo.TGUID  where " + strWhere);
            }
            else
            {
                strSql.Append("SELECT top " + pageSize + "  dbo.t_Comp_Yeji.TGuid, dbo.t_Comp_Yeji.PGuid, dbo.t_Comp_Yeji.Xiangmumingcheng, dbo.t_Comp_Yeji.Xiangmuleixing, dbo.t_Comp_Yeji.Hetongjia,dbo.t_Comp_Yeji.Jiesuanjia, dbo.t_Comp_Yeji.Jishudengji, dbo.t_Comp_Yeji.Hetongduanmingcheng, dbo.t_Comp_Yeji.Kaigongriqi, dbo.t_Comp_Yeji.Jungongriqi, dbo.t_Comp_Yeji.Hetongduankaishizhuanghao, dbo.t_Comp_Yeji.Hetongduanjiesuzhuanghao, dbo.t_Comp_Yeji.zhiliangpingdingqingkuang, dbo.t_Comp_Yeji.suozaishengfen, dbo.t_Comp_Yeji.zhuyaogongchengliang, dbo.t_Comp_Yeji.jianshezhuangtai, dbo.t_Comp_Yeji.xinxitianjiashijian, dbo.t_Comp_Yeji.Gongchengleibie, dbo.t_Comp_Yeji.Chengbaoleixing, dbo.t_Comp_BasicInfo.zhucedi, dbo.t_Comp_BasicInfo.qiyemingcheng FROM dbo.t_Comp_Yeji INNER JOIN dbo.t_Comp_BasicInfo ON dbo.t_Comp_Yeji.PGuid = dbo.t_Comp_BasicInfo.TGUID  where dbo.t_Comp_Yeji.TGuid not in(select top " + pageSize * (currentPageIndex - 1) + " TGuid from dbo.t_Comp_Yeji) and " + strWhere);
            }
            return DbHelperOleDb.Query(strSql.ToString());

        }


        /// <summary>
        /// 添加的特殊情况
        /// </summary>
        /// <param name="FieldID"></param>
        /// <param name="showFields"></param>
        /// <param name="DataTableName"></param>
        /// <param name="pageSize"></param>
        /// <param name="currentPageIndex"></param>
        /// <param name="strWhere"></param>
        /// <param name="OrderFieldID"></param>
        /// <param name="OrderFlag"></param>
        /// <returns></returns>
        public DataSet GetWherePagelist1(string FieldID, string showFields, string DataTableName, int pageSize, int currentPageIndex, string strWhere, string OrderFieldID, int OrderFlag, string zhouzhanadd)
        {
            string strOrder = " desc ";
            StringBuilder strSql = new StringBuilder();
            if (OrderFlag == 0)
            {
                strOrder = " asc ";
            }
            if (currentPageIndex <= 1)
            {
                strSql.Append("select top " + pageSize + " " + showFields + " FROM " + DataTableName + " where " + strWhere + " order by " + OrderFieldID + strOrder);
            }
            else
            {
                strSql.Append("select top " + pageSize + " " + showFields);
                strSql.Append(" FROM " + DataTableName + " where " + FieldID + " not in(select top " + pageSize * (currentPageIndex - 1) + " " + zhouzhanadd + " from " + DataTableName + " where " + strWhere + " order by " + OrderFieldID + strOrder + " ) and " + strWhere + " order by " + OrderFieldID + strOrder);
                // strSql.Append(" FROM " + DataTableName + " where " + FieldID + " not in(select top " + pageSize * (currentPageIndex - 1) + " " + FieldID + " from " + DataTableName + " where " + strWhere + " ) and " + strWhere + " order by " + OrderFieldID + strOrder);

            }
            return DbHelperOleDb.Query(strSql.ToString());

        }
    }
}
